package com.bw.order.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;

import com.bw.common.utils.R;
import com.bw.order.config.IdWorker;
import com.bw.order.feign.HouseFeign;
import com.bw.order.to.HouseNewroomTo;
import com.bw.order.vo.HouseUserVo;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bw.common.utils.PageUtils;
import com.bw.common.utils.Query;

import com.bw.order.dao.HousePurchaseOrderDao;
import com.bw.order.entity.HousePurchaseOrderEntity;
import com.bw.order.service.HousePurchaseOrderService;


@Service("housePurchaseOrderService")
public class HousePurchaseOrderServiceImpl extends ServiceImpl<HousePurchaseOrderDao, HousePurchaseOrderEntity> implements HousePurchaseOrderService {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private HouseFeign houseFeign;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private HousePurchaseOrderDao housePurchaseOrderDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<HousePurchaseOrderEntity> page = this.page(
                new Query<HousePurchaseOrderEntity>().getPage(params),
                new QueryWrapper<HousePurchaseOrderEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public R add(Map<String, Object> params) {
        //uuid查看redis数据是否失效
        String uuids = (String) params.get("uuids");
        //获取用户id查看是否登录或者失效
        String uid = (String) params.get("uid");
        //获取用户登录信息
        String user = redisTemplate.opsForValue().get(uid);
        //获取订单编号
        String s = redisTemplate.opsForValue().get(uuids);
        //判断段是否登录或者登录过期
        if (s!=null&&!s.equals("")){
            HouseUserVo houseUserVo = JSON.parseObject(user, new TypeReference<HouseUserVo>() {
            });
            if (s!=null&&!s.equals("")){
                HouseNewroomTo house = houseFeign.findone(s);
                HousePurchaseOrderEntity orderEntity = new HousePurchaseOrderEntity();
                if (house!=null){
                    long oid = idWorker.nextId();
                    orderEntity.setPurchaseOid(oid);
                    orderEntity.setUserPhone(houseUserVo.getUserPhone());
                    orderEntity.setPurchasePrice(house.getRoomPrice());
                    orderEntity.setHousePrice(house.getRoomSquarePrice());
                    orderEntity.setCreateTime(new Date());
                    orderEntity.setPurchaseStatus(0);
                    orderEntity.setRoomId(Long.valueOf(s));
                    orderEntity.setUserId(houseUserVo.getUserPhone());
                    housePurchaseOrderDao.add(orderEntity);
                    return R.ok();
                }else {
                    System.out.println("信息错误");
                    return R.error(250,"不存在该房屋");
                }
            }else {
                System.out.println("该订单失效");
                return R.error(404,"111");
            }
        }else {
            System.out.println("登录失效或未登录");
            return R.error(403,"无发购买，重新登录");
        }
        //如果是空值可能是登录失效或者未登录

    }

}